// pages/clickMe/coupon/index.js
import api from '../../../api/index'
import { userInfoStore } from '../../../store/userInfo'
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    dataList: [],
    uploading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    this.setData({ dataList: [], uploading: false, type: options.type}, () => {
      self.getList();
    })
  },

  onShow: function(){
    
  },

  selectItem:function(e){
    let self=this;
    this.setData({ dataList: [], uploading: false, type: e.currentTarget.dataset.type},()=>{
      self.getList();
    });
  },

  showDetail:function(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'detail?id=' + id,
    })
  },

  getList: function (){
    let self=this;
    let param = {
      openId: userInfoStore.userInfo.openId,
      type: this.data.type
    }

    // if (this.data.type==99){
    //   param = Object.assign(param, { type: 4 })
    // }
    api.getusercouponlist(param).then(res => {
      console.log(res);
      let { result } = res;
      if (self.data.uploading) {
        self.data.dataList.push(...result);
        self.setData({
          dataList: self.data.dataList
        })
      } else {
        self.setData({
          dataList: result
        })
      }
      console.log("complete");
      self.setData({ uploading: false });
      wx.stopPullDownRefresh();
    }, err => {
      console.log("err");
      self.setData({ uploading: false });
      wx.stopPullDownRefresh();
    });
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self=this;
    this.setData({ dataList: [], uploading:false},()=>{
      self.getList();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.uploading) {
      var self=this;
      this.setData({ uploading: true },()=>{
        
        // var current = parseInt(self.data.result.current);
        // var pages = self.data.result.pages;
        // if (current < pages) {
          
        // }

      });
      
    }
  }
})